package web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import domain.User;
import repository.UserDAO;

@WebServlet("/UserMod")
public class UserMod extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public UserMod() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		UserDAO userDao = null;
		try {
			userDao = new UserDAO();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		HttpSession session = request.getSession();
		String Id= (String) session.getAttribute("loginUser");
		System.out.println(Id);
		ResultSet rs = userDao.query(Id);	
		System.out.println(rs);
		User user = new User();
		try {
			while(rs.next()){
				user.setUserId(rs.getString("用户Id"));
				user.setUserName(rs.getString("用户名"));
				user.setPwd(rs.getString("密码"));
				user.setIdentity(rs.getString("用户类型"));
				user.setPhnumber(rs.getString("电话号码"));
				user.setAdamic(rs.getString("学院名称"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		User allprop = new User();
		allprop.setUserId(user.getUserId());
		if(request.getParameter("name")=="") {
			allprop.setUserName(user.getUserName());
		}else {
			allprop.setUserName(request.getParameter("name"));
		}
		if(request.getParameter("pwd")=="") {
			allprop.setPwd(user.getPwd());
		}else {
			allprop.setPwd(request.getParameter("pwd"));
		}
		if(request.getParameter("identity")=="") {
			allprop.setIdentity(user.getIdentity());
		}else {
			allprop.setIdentity(request.getParameter("identity"));
		}
		if(request.getParameter("number")=="") {
			allprop.setPhnumber(user.getPhnumber());
		}else {
			allprop.setPhnumber(request.getParameter("number"));
		}
		if(request.getParameter("acdem")=="") {
			allprop.setAdamic(user.getAdamic());
		}else {
			allprop.setAdamic(request.getParameter("acdem"));
		}
		userDao.update(allprop);
		request.getRequestDispatcher("userInfor.jsp").forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
